﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;

using XMK.Framework.DataAccess;
using XMK.Tadong.Model;
using XMK.Utils;
using XMK.Framework.Model;
using XMK.Tadong.Model.Cond;
namespace XMK.Tadong.Data
{
   public  class Tb_SpaceData
    {
        /// <summary>
        /// 数据库连接
        /// </summary>
        protected DbProvider dbProvider;

        public Tb_SpaceData(DbProvider dbProvider)
        {
            this.dbProvider = dbProvider;
        }
        /// <summary>
        /// 空间列表提取
        /// </summary>
        /// <returns></returns>
        public List<TB_SPACEInfo> GetSpaceList()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM TB_SPACE");

            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());
            cmd.DbCmdInfo.CommandType = CommandType.Text;
            List<TB_SPACEInfo> spaceList = DbManager.GetInfoList<TB_SPACEInfo>(cmd,
               delegate(out TB_SPACEInfo space, DataRow row)
               {
                   space = new TB_SPACEInfo
                   {
                       SPACE_ID = ParseHelper.Parse<decimal>(row["SPACE_ID"]),
                       SPACE_NAME = ParseHelper.Parse<string>(row["SPACE_NAME"]),
                       SPACE_STS = ParseHelper.Parse<decimal>(row["SPACE_STS"]),
                       SPACE_TYPE = ParseHelper.Parse<decimal>(row["SPACE_TYPE"]),
                       OBJ_ID = ParseHelper.Parse<decimal>(row["OBJ_ID"]),
                       SPACE_DESC = ParseHelper.Parse<string>(row["SPACE_DESC"]),
                       CREATE_TIME = ParseHelper.Parse<DateTime>(row["CREATE_TIME"]),
                       UPD_TIME = ParseHelper.Parse<DateTime>(row["UPD_TIME"])
                     
                   };
               });
            return spaceList;
        }

        /// <summary>
        /// 分类查看查询数据
        /// </summary>
        /// <param name="teamId"></param>
        /// <returns></returns>
        public DataTable getXmkClassFlie(string personId,string teamId, string FOLDER_ID, string classType,string spaceType)
        {
            StringBuilder strSql = new StringBuilder();
            string dbspace;
            string sqlwhere= " WHERE FOLDER_ID='" + FOLDER_ID + "'";
            if (spaceType == "10")
            {
                dbspace = "TB_PERSON_FILE";
                sqlwhere += " and USER_ID='" + personId + "'";
            }
            else if (spaceType == "20")
            {
                dbspace = "TB_TEAM_FILE";
                sqlwhere += " and TEAM_ID=" + teamId;
            }
            else
            {
                dbspace = "TB_XMK_FILE";
            }
            if (classType == "CREATE_TIME")
            {
                strSql.Append("select FILE_ID,FILE_NAME," + classType + " from "+dbspace+sqlwhere+" order by " + classType + "");
            }
            else
            {
                strSql.Append("select FILE_ID,FILE_NAME,CREATE_TIME," + classType + " from "+dbspace+sqlwhere+" order by " + classType + "");
            }


            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());

            cmd.DbCmdInfo.CommandType = CommandType.Text;
            DataTable ds = DbHelper.ExecFullDataTable(cmd.DbProvider, cmd.DbCmdInfo);
            if (ds != null)
            {
                return ds.Copy();
            }
            return null;
        }

        /// <summary>
        /// 分类查看明细数据
        /// </summary>
        /// <param name="teamId"></param>
        /// <returns></returns>
        public DataTable getXmkClassDetailFile(string personId, string teamId, string sqlwhere, string spaceType)
        {
            StringBuilder strSql = new StringBuilder();
            string dbspace;
            if (spaceType == "10")
            {
                dbspace = "TB_PERSON_FILE";
                sqlwhere += " and USER_ID='" + personId + "'";
            }
            else if (spaceType == "20")
            {
                dbspace = "TB_TEAM_FILE";
                sqlwhere += " and TEAM_ID=" + teamId ;
            }
            else
            {
                dbspace = "TB_XMK_FILE";
            }
            strSql.Append("select FILE_ID as id,FILE_NAME,fILE_SIZE,CREATE_TIME,SAVE_PATH from " + dbspace + "  WHERE " + sqlwhere);

            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());

            cmd.DbCmdInfo.CommandType = CommandType.Text;
            DataTable ds = DbHelper.ExecFullDataTable(cmd.DbProvider, cmd.DbCmdInfo);
            if (ds != null)
            {
                return ds.Copy();
            }
            return null;
        } 
        //#region test
    }
}
